package com.yunfei.api.v1;

import com.yunfei.api.v1.magerIndcInfo.param.CancelZuofeiParam;
import com.yunfei.api.v1.magerIndcInfo.param.CreateAgainParam;
import com.yunfei.api.v1.param.ContractCancelParam;
import com.yunfei.api.v1.service.CreateZuoFeiXieYiService;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.contants.QueryHeTongSignInfoContant;
import com.yunfei.api.v2.param.ContractIdParam;
import com.yunfei.api.v2.param.CurrentUserParam;
import com.yunfei.api.v2.param.ReserveNoParam;
import com.yunfei.api.v2.param.heTongSign.HeTongSignParam;
import com.yunfei.api.v2.result.jiaGaiResult.AutoJiaGaiResult;
import com.yunfei.api.v2.service.ContractSignService;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.AutoJiaGaiZhangMapper;
import com.yunfei.mapper.aiiidb.GetStatusMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.service.RedisService;
import com.yunfei.yunhetong.elecContBeneAcctDtl.bo.ElecContBeneAcctDtlBo;
import com.yunfei.yunhetong.elecContBeneAcctDtl.service.ElecContBeneAcctDtlService;
import com.yunfei.yunhetong.elecContCancelDtl.bo.ElecContCancelDtlBo;
import com.yunfei.yunhetong.elecContCancelDtl.service.ElecContCancelDtlService;
import com.yunfei.yunhetong.elecContOperLog.bo.ElecContOperLogBo;
import com.yunfei.yunhetong.elecContOperLog.service.ElecContOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 合同签署管理后台接口
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/v1/contractSignManager")
@Api(value = QueryHeTongSignInfoContant.CONTRACT_SIGN_MANAGER_NAMESPACE,tags =  QueryHeTongSignInfoContant.CONTRACT_SIGN_MANAGER_NAMESPACE )
public class ContractSignManagerApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private ElecContBeneAcctDtlService elecContBeneAcctDtlService;
    @Autowired
    private ElecContOperLogService elecContOperLogService;
    @Autowired
    private ElecContCancelDtlService elecContCancelDtlService;
    @Autowired
    private CreateZuoFeiXieYiService createZuoFeiXieYiService;
    @Autowired
    private AutoJiaGaiZhangMapper autoJiaGaiZhangMapper;
    @Autowired
    private ContractSignService contractSignService;


    /**
     * @Description: 取消作废
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/cancel/zuofei")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.CANCEL_ZUOFEI,notes = QueryHeTongSignInfoContant.CANCEL_ZUOFEI,response = ResultModel.class)
    @ResponseBody
    public Object cancelZuoFei(CancelZuofeiParam param){
        //更新法人信息
        ResultModel resultModel=new ResultModel();
        try{
            String contractId=param.getContractId();
            String contractNum=param.getContractNum();
            String operId=param.getOperatorId();
            ElecContBeneAcctDtlBo elecContBeneAcctDtlBo=new ElecContBeneAcctDtlBo();
            elecContBeneAcctDtlBo.setContractId(param.getContractId());
            elecContBeneAcctDtlBo.setContractSts("3");//更新管理人签署完成
            elecContBeneAcctDtlService.update(elecContBeneAcctDtlBo);
            //记录操作日志
            ElecContOperLogBo elecContOperLogBo=new ElecContOperLogBo();
            elecContOperLogBo.setContractId(param.getContractId());
            elecContOperLogBo.setContractNum(contractNum);
            elecContOperLogBo.setOperId(operId);
            elecContOperLogBo.setOperTyp("取消作废协议签署");
            elecContOperLogService.add(elecContOperLogBo);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
     * @Description: 生成作废协议
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/create/zuoFeiXieYi")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.CREATE_ZUOFEI_XIEYI,notes = QueryHeTongSignInfoContant.CREATE_ZUOFEI_XIEYI,response = ResultModel.class)
    @ResponseBody
    public Object createZuoFeiXieYi(ReserveNoParam param, CurrentUserParam currentUserParam, ContractIdParam contractIdParam, ContractCancelParam contractCancelParam){
        ResultModel resultModel=new ResultModel();
        try{
          //调用api生成作废协议
            Object result=createZuoFeiXieYiService.create(resultModel,param,currentUserParam,contractIdParam,contractCancelParam);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }


    /**
     * @Description:再次生成
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/create/again")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.CREATE_AGAIN,notes = QueryHeTongSignInfoContant.CREATE_AGAIN,response = ResultModel.class)
    @ResponseBody
    public Object createAgain(CreateAgainParam param,ReserveNoParam reserveNoParam, CurrentUserParam currentUserParam, ContractIdParam contractIdParam, ContractCancelParam contractCancelParam){
        //更新法人信息
        ResultModel resultModel=new ResultModel();
        try{
            String contractId=param.getContractId();
            String reserveNo=reserveNoParam.getReserveNo();
            String operId=param.getOperatorId();
            ElecContCancelDtlBo elecContCancelDtlBo=new ElecContCancelDtlBo();
            elecContCancelDtlBo.setCancelContractId(param.getCancelContractId());
            elecContCancelDtlBo.setContractSts("7");
            //更新管理人签署完成
            elecContCancelDtlService.update(elecContCancelDtlBo);
            //和生成作废协议一样
            Object result=createZuoFeiXieYiService.create(resultModel,reserveNoParam,currentUserParam,contractIdParam,contractCancelParam);
            //提送消息消息
            createZuoFeiXieYiService.sendMessage(contractId,reserveNo);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description:合同签署管理页面-》合同作废-》加盖公章
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/jiaGai/gongzhang")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.CONTRACT_ZUOFEI_GAIZHANG,notes = QueryHeTongSignInfoContant.CONTRACT_ZUOFEI_GAIZHANG_NOTE,response = ResultModel.class)
    @ResponseBody
    public Object jiaGaiGongZhang(CreateAgainParam param,ReserveNoParam reserveNoParam, CurrentUserParam currentUserParam, ContractIdParam contractIdParam, ContractCancelParam contractCancelParam){
        //更新法人信息
        ResultModel resultModel=new ResultModel();
        try{
            String contractId=param.getContractId();
            String reserveNo=reserveNoParam.getReserveNo();
            String operId=param.getOperatorId();
            //获取需要加盖公章生成的接口
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("reserveNo",reserveNo);
            List<AutoJiaGaiResult> autoJiaGaiResult= ( List<AutoJiaGaiResult>) autoJiaGaiZhangMapper.contractZuoFeiJiaGaiGongZhangData(queryMap);
            //调用合同签署接口
            for(int i=0;i<autoJiaGaiResult.size();i++){
                String caCustId=autoJiaGaiResult.get(i).getCaCustId();
                String magerId=autoJiaGaiResult.get(i).getMagerId();
                HeTongSignParam faRenParam = new HeTongSignParam();
                faRenParam.setIdType(YunHeTongApiContant.ID_TYPE_DEFAULT);
                faRenParam.setIdContent(contractId);
                faRenParam.setSignerId(caCustId);
                faRenParam.setMoulageId(magerId);
                faRenParam.setSealClass(YunHeTongApiContant.SET_SEAL_CLASS);//带时间样式的签章
                ResultModel farenResult=contractSignService.sign(faRenParam);
                if(farenResult.getCode()!=0){
                    JSONObject jsonObject= (JSONObject) farenResult.getData();
                    resultModel.setCode(20105);
                    resultModel.setMessage(jsonObject.get("msg")+"");
                    return resultModel;
                }
            }
            //记录操作日志
            ElecContOperLogBo elecContOperLogBo=new ElecContOperLogBo();
            elecContOperLogBo.setContractId(param.getContractId());
            elecContOperLogBo.setContractNum(param.getCancelContractNum());//对应查看列表的CANCEL_CONTRACT_NUM
            elecContOperLogBo.setOperId(operId);
            elecContOperLogBo.setOperTyp("加盖管理人公章");
            elecContOperLogService.add(elecContOperLogBo);
            //提送消息消息
            createZuoFeiXieYiService.sendMessage(contractId,reserveNo);
            resultModel.setData("操作成功");
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            return resultModel;
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

}
